package com.hongkuncheng.cms.helper;

import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Cookie帮助类
 *
 * @author 洪坤成
 */
public class CookieHelper {

    public static void SetCookie(HttpServletResponse response, String name, String value, boolean isEncrypt) {
        Cookie cookie = null;
        try {
            cookie = new Cookie(URLEncoder.encode(URLEncoder.encode(name, "UTF-8"), "UTF-8"), !isEncrypt ? StringHelper.enUnicode(value) : SecurityHelper.aesEncrypt(value));
            cookie.setPath("/");
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    public static void SetCookie(HttpServletResponse response, String name, String value, int expiry, boolean isEncrypt) {
        Cookie cookie = null;
        try {
            cookie = new Cookie(URLEncoder.encode(name, "UTF-8"), !isEncrypt ? StringHelper.enUnicode(value) : SecurityHelper.aesEncrypt(value));
            cookie.setMaxAge(expiry);
            cookie.setPath("/");
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    public static void SetCookie(HttpServletResponse response, String name, String value, String path, boolean isEncrypt) {
        Cookie cookie = null;
        try {
            cookie = new Cookie(URLEncoder.encode(name, "UTF-8"), !isEncrypt ? StringHelper.enUnicode(value) : SecurityHelper.aesEncrypt(value));
            cookie.setPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    public static void SetCookie(HttpServletResponse response, String name, String value, int expiry, String path, boolean isEncrypt) {
        Cookie cookie = null;
        try {
            cookie = new Cookie(URLEncoder.encode(name, "UTF-8"), !isEncrypt ? StringHelper.enUnicode(value) : SecurityHelper.aesEncrypt(value));
            cookie.setMaxAge(expiry);
            cookie.setPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    public static void RemoveCookie(HttpServletResponse response, String name) {
        Cookie cookie = null;
        try {
            cookie = new Cookie(URLEncoder.encode(name, "UTF-8"), null);
            cookie.setMaxAge(0);
            cookie.setPath("/");
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    public static void RemoveCookie(HttpServletResponse response, String name, String path) {
        Cookie cookie = null;
        try {
            cookie = new Cookie(URLEncoder.encode(name, "UTF-8"), null);
            cookie.setMaxAge(0);
            cookie.setPath(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    public static String GetCookie(HttpServletRequest request, String name, boolean isDecrypt) {
        String value = null;
        Cookie[] cookies = request.getCookies();
        try {
            for (int i = 0; cookies != null && i < cookies.length; i++) {
                if (cookies[i].getName().equals(URLEncoder.encode(name, "UTF-8"))) {
                    value = cookies[i].getValue();
                    if (value != null && value != "") value = !isDecrypt ? StringHelper.deUnicode(value) : SecurityHelper.aesDecrypt(value);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

}
